package org.dromara.quick.chat.controller;

import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.encrypt.properties.EncryptorProperties;
import org.dromara.common.encrypt.utils.EncryptUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.quick.chat.model.bo.query.ImSocialPrivacySettingBO;
import org.dromara.quick.chat.model.dto.ChatRecordPasswordDTO;
import org.dromara.quick.chat.model.dto.IntegralPasswordDTO;
import org.dromara.quick.chat.service.ImSocialPrivacySettingService;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Stars
 * @description 针对表【im_social_privacy_setting(社交软件中的隐私设置表，用于存储用户的隐私设置信息)】的数据库操作Service
 * @Date: 2025/3/6 22:33
 */
@Tag(name = "社交软件中的隐私设置")
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/social/privacy/setting")
public class ImSocialPrivacySettingController extends BaseController {

    private final ImSocialPrivacySettingService imSocialPrivacySettingService;
    private final EncryptorProperties encryptorProperties;

    /**
     * 查询隐私设置
     */
    @Operation(summary = "查询隐私设置")
    @GetMapping("/list")
    public R<?> selectPrivacySetting() {
        return R.ok(imSocialPrivacySettingService.selectPrivacySetting(LoginHelper.getUserId()));
    }

    /**
     * 更改隐私设置
     */
    @Operation(summary = "更改隐私设置")
    @PostMapping("/update")
    public R<?> updatePrivacySetting(@RequestBody ImSocialPrivacySettingBO imSocialPrivacySettingBO) {
        imSocialPrivacySettingService.updatePrivacySetting(imSocialPrivacySettingBO);
        return R.ok();
    }

    /**
     * 更改聊天记录保密密码
     */
    @Operation(summary = "更改聊天记录保密密码")
    @PostMapping("/update/chat/password")
    public R<?> updateChatRecordPassword(@RequestBody String body) {
        body = EncryptUtils.decryptByAes(body, encryptorProperties.getAppKey());
        ChatRecordPasswordDTO chatRecordPasswordDTO = JSONUtil.toBean(body, ChatRecordPasswordDTO.class);
        if (!chatRecordPasswordDTO.getPassword().equals(chatRecordPasswordDTO.getConfirmPassword())) {
            throw new ServiceException("两次密码不一致");
        }
        imSocialPrivacySettingService.updateChatRecordPassword(chatRecordPasswordDTO);
        return R.ok();
    }

    /**
     * 设置积分支付密码
     */
    @Operation(summary = "设置积分支付密码")
    @PostMapping("/update/integral/password")
    public R<?> updateIntegralPassword(@RequestBody String body) {
        body = EncryptUtils.decryptByAes(body, encryptorProperties.getAppKey());
        IntegralPasswordDTO integralPasswordDTO = JSONUtil.toBean(body, IntegralPasswordDTO.class);
        if (!integralPasswordDTO.getPassword().equals(integralPasswordDTO.getConfirmPassword())) {
            throw new ServiceException("两次密码不一致");
        }
        imSocialPrivacySettingService.updateIntegralPassword(integralPasswordDTO);
        return R.ok();
    }
}
